package com.spic.business.informatization.fixedContent.controller;

import com.spic.business.common.core.domain.TreeNode;
import com.spic.business.common.core.utils.TreeUtils;
import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.informatization.fixedContent.domain.FixedContent;
import com.spic.business.informatization.fixedContent.service.IFixedContentService;
import com.spic.common.core.domain.R;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.api.RemoteUserService;
import com.spic.system.api.domain.SysUser;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 固定内容Controller
 *
 * @author spic
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/informatization/fixedcontent")
public class FixedContentController extends BaseController
{
    @Autowired
    private IFixedContentService trainCoursewareService;

    @Autowired
    RemoteUserService userService;

    /**
     * 查询固定内容列表
     */
    @PreAuthorize(hasPermi = "business/informatization:fixedContent:list")
    @GetMapping("/list")
    public TableDataInfo list(FixedContent fixedContent)
    {
        startPage();
        List<FixedContent> list = trainCoursewareService.selectFixedContentList(fixedContent);
        return getDataTable(list);
    }
    @GetMapping("/dirtree")
    public List<TreeNode> dirTree(){
        Integer dir = 1;
        FixedContent fixedContent = new FixedContent();
        fixedContent.setDir(dir);
        List<FixedContent> list = trainCoursewareService.selectFixedContentList(fixedContent);
        List<TreeNode> treeNodes = list.stream().map(item -> {
            TreeNode treeNode = new TreeNode();
            treeNode.setId(item.getId());
            treeNode.setPid(item.getPid());
            treeNode.setLabel(item.getFileName());
            treeNode.setChildren(new ArrayList<TreeNode>());
            return treeNode;
        }).collect(Collectors.toList());
        List<TreeNode> nodeList = TreeUtils.build(treeNodes, "0");
        return nodeList;
    }

    @GetMapping("fileTree")
    public List<TreeNode> dirFileTree(@RequestParam String fileType) {
        Long userId = SecurityUtils.getUserId();
        R<SysUser> userResp = userService.getUserById(userId);
        if (userResp != null && userResp.getCode() == HttpStatus.SC_OK) {
            SysUser data = userResp.getData();
            Long deptId = data == null ? null : data.getDeptId();
            FixedContent fixedContent = new FixedContent();
            fixedContent.setFileType(fileType);
            fixedContent.setDept(deptId);
            List<FixedContent> list = trainCoursewareService.selectFixedContentList(fixedContent);
            if (list == null || list.size() == 0) {
                return new ArrayList<>();
            }
            List<TreeNode> treeNodes = list.stream().map(item -> {
                TreeNode treeNode = new TreeNode();
                treeNode.setId(item.getId());
                treeNode.setPid(item.getPid());
                treeNode.setLabel(item.getFileName());
                treeNode.setChildren(new ArrayList<TreeNode>());
                return treeNode;
            }).collect(Collectors.toList());
            List<TreeNode> nodeList = TreeUtils.build(treeNodes);
            return nodeList;
        }
        return new ArrayList<>();
    }

    /**
     * 导出固定内容列表
     */
    @PreAuthorize(hasPermi = "business/informatization:fixedContent:export")
    @Log(title = "固定内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FixedContent fixedContent) throws IOException
    {
        List<FixedContent> list = trainCoursewareService.selectFixedContentList(fixedContent);
        ExcelUtil<FixedContent> util = new BusinessExcelUtil<FixedContent>(FixedContent.class);
        util.exportExcel(response, list, "固定内容数据");
    }


    /**
     * 获取固定内容详细信息
     */
    @PreAuthorize(hasPermi = "business/informatization:fixedContent:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(trainCoursewareService.selectFixedContentById(id));
    }

    /**
     * 新增固定内容
     */
    @PreAuthorize(hasPermi = "business/informatization:fixedContent:add")
    @Log(title = "固定内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FixedContent fixedContent)
    {
        Long userId = SecurityUtils.getUserId();
        R<SysUser> userResp = userService.getUserById(userId);
        if (userResp != null && userResp.getCode() == HttpStatus.SC_OK) {
            SysUser data = userResp.getData();
            Long deptId = data == null ? null : data.getDeptId();
            fixedContent.setDeptId(deptId);
        }
        return toAjax(trainCoursewareService.insertFixedContent(fixedContent));
    }

    /**
     * 修改固定内容
     */
    @PreAuthorize(hasPermi = "business/informatization:fixedContent:edit")
    @Log(title = "固定内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FixedContent fixedContent)
    {
        return toAjax(trainCoursewareService.updateFixedContent(fixedContent));
    }

    /**
     * 删除固定内容
     */
    @PreAuthorize(hasPermi = "business/informatization:fixedContent:remove")
    @Log(title = "固定内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(trainCoursewareService.deleteFixedContentByIds(ids));
    }
}
